<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" type="text/css" href="../../css/mui.min.css" />
		<script src="../../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
		<style>
			.main_content p {
				font-size: 1em;
				color: black;
				text-indent: 2em;
				padding: 6px;
			}
			.main_content img {
				padding: 8px;
			}
			.sub_header {
				text-align: center;
				background-color: rgba(0, 188, 0, 0.7);
				height: 36px;
				margin-top: 8px;
				margin-bottom: 8px;
			}
			.sub_header h3 {
				color: white;
				position: relative;
				top: 20%;
				transform: translateY(-50%);
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">Java 基础</h1>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded" style="margin: 5px;">
				<div class="sub_header">
					<h3>数据类型转换</h3>
				</div>
				<div class="main_content">
					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">在java中强制类型转换分为基本数据类型和引用数据类型两种，这里为大家介绍第二种，也就是引用数据类型的强制类型转换。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">在Java中由于继承和向上转型，子类可以非常自然地转换成父类，但是父类转换成子类则需要强制转换。因为子类拥有比父类更多的属性、更强的功能，所以父类转换为子类需要强制。那么，是不是只要是父类转换为子类就会成功呢?其实不然，他们之间的强制类型转换是有条件的。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">当我们用一个类型的构造器构造出一个对象时，这个对象的类型就已经确定的，也就说它的本质是不会再发生变化了。在Java中我们可以通过继承、向上转型的关系使用父类类型来引用它，这个时候我们是使用功能较弱的类型引用功能较强的对象，这是可行的。但是将功能较弱的类型强制转功能较强的对象时，就不一定可以行了。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">举个例子来说明。比如系统中存在Father、Son两个对象。首先我们先构造一个Son对象，然后用一个Father类型变量引用它：</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">Father father = new Son();</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">在这里Son 对象实例被向上转型为father了，但是请注意这个Son对象实例在内存中的本质还是Son类型的，只不过它的能力临时被消弱了而已，如果我们想变强怎么办?将其对象类型还原!</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">Son son = (Son)father;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">这条语句是可行的，其实father引用仍然是Father类型的，只不过是将它的能力加强了，将其加强后转交给son引用了，Son对象实例在son的变量的引用下，恢复真身，可以使用全部功能了。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">前面提到父类强制转换成子类并不是总是成功，那么在什么情况下它会失效呢?</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">当引用类型的真实身份是父类本身的类型时，强制类型转换就会产生错误。例如：</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">Father father = new Father();</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">Son son = (Son) father;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">这个系统会抛出ClassCastException异常信息。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">所以编译器在编译时只会检查类型之间是否存在继承关系，有则通过;而在运行时就会检查它的真实类型，是则通过，否则抛出ClassCastException异常。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">所以在继承中，子类可以自动转型为父类，但是父类强制转换为子类时只有当引用类型真正的身份为子类时才会强制转换成功，否则失败。</div>

					<div style="margin: 0px; padding: 0px; font-family: Arial, Verdana, Helvetica, sans-serif, 宋体; font-size: 14px; line-height: 25.2000007629395px; text-indent: 20.25pt; background-color: rgb(250, 247, 240);">&nbsp;</div>

				</div>
			</div>
		</div>

	</body>

</html>